package atguigu.atomic;
import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceDemo {
    static AtomicReference<UserInfo> atomicReference;

    public static void main(String[] args) {
        //原引用
        UserInfo oldUser = new UserInfo("老王", 35);
        atomicReference = new AtomicReference<>(oldUser);

        //新引用
        UserInfo updateUser = new UserInfo("小宋", 21);
        atomicReference.compareAndSet(oldUser, updateUser);

        System.out.println("使用compareAndSet()替换原有引用后的结果：" + atomicReference.get());
        System.out.println("原引用：" + oldUser);
    }

    static class UserInfo {
        private String name;
        private int age;

        public UserInfo(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "UserInfo{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}